package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.ActivityDao;
import com.example.spark.entity.Activity;
import com.example.spark.entity.Direction;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.ActivityService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;


@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityDao, Activity> implements ActivityService {

    @Autowired
    private ActivityDao activityDao;

    @Resource
    MinioCloudStorageService minioCloudStorageService;

    //查询活动
    @Override
    public CommonResult selectByType(int type) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);

        List<Map<String, Object>> maps = activityDao.selectMaps(queryWrapper);
        for (Map<String, Object> map : maps) {
            map.put("id",map.get("id").toString());
            System.out.println(map.get("id"));
        }

        if (!maps.isEmpty()) {
            return new CommonResult(200, "查询成功", maps);
        }
        return new CommonResult(200, "查询失败");
    }

    //添加活动
    @Override
    public CommonResult addActivity(Activity activity) {
        long id = getId();
        activity.setId(id);
        activityDao.insert(activity);
        return new CommonResult<>(200, "添加成功");
    }

    //删除活动
    @Override
    public CommonResult deleteActivity(Long id) {
        Activity activity = activityDao.selectById(id);

        if (activity != null) {
            activityDao.deleteById(id);
            return new CommonResult(200, "删除成功");
        }
        return new CommonResult(200, "删除失败");
    }

    //修改活动
    @Override
    public CommonResult updateActivity(Activity activity) {
        Long id = activity.getId();
        Activity activity1 = activityDao.selectById(id);
        if (activity1 != null) {
            activityDao.updateById(activity);
            return new CommonResult(200, "修改成功", activity);
        }
        return new CommonResult(200,"修改失败");
    }

    //图片处理
    @Override
    public CommonResult addPhoto(MultipartFile file, Long id) throws IOException {

        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String url = minioCloudStorageService.uploadPhotoSuffix(file.getBytes(), extension, String.valueOf(getId()));

        Activity activity = activityDao.selectById(id);
        activity.setPhoto(url);
        int is = activityDao.updateById(activity);
        if (is > 0) {
            return new CommonResult(200, "添加图片成功");
        }
        return new CommonResult(200, "添加图片失败");
    }

}
